package com.example.diethealthsystem.controller;

import com.example.diethealthsystem.entity.UserHealthIndex;
import com.example.diethealthsystem.service.UserHealthIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/userhealthindices")
public class UserHealthIndexController {

    private final UserHealthIndexService userHealthIndexService;

    @Autowired
    public UserHealthIndexController(UserHealthIndexService userHealthIndexService) {
        this.userHealthIndexService = userHealthIndexService;
    }

    @GetMapping
    public ResponseEntity<List<UserHealthIndex>> getAllUserHealthIndices() {
        List<UserHealthIndex> userHealthIndices = userHealthIndexService.findAll();
        return new ResponseEntity<>(userHealthIndices, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserHealthIndex> getUserHealthIndexById(@PathVariable Long id) {
        return userHealthIndexService.findById(id)
                .map(userHealthIndex -> new ResponseEntity<>(userHealthIndex, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PostMapping
    public ResponseEntity<UserHealthIndex> createUserHealthIndex(@RequestBody UserHealthIndex userHealthIndex) {
        UserHealthIndex createdUserHealthIndex = userHealthIndexService.save(userHealthIndex);
        return new ResponseEntity<>(createdUserHealthIndex, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<UserHealthIndex> updateUserHealthIndex(@PathVariable Long id, @RequestBody UserHealthIndex userHealthIndex) {
        return userHealthIndexService.findById(id)
                .map(existingUserHealthIndex -> {
                    existingUserHealthIndex.setUser(userHealthIndex.getUser());
                    existingUserHealthIndex.setHeight(userHealthIndex.getHeight());
                    existingUserHealthIndex.setWeight(userHealthIndex.getWeight());
                    existingUserHealthIndex.setBodyFat(userHealthIndex.getBodyFat());
                    existingUserHealthIndex.setBmi(userHealthIndex.getBmi());
                    existingUserHealthIndex.setRecordDate(userHealthIndex.getRecordDate());
                    existingUserHealthIndex.setHeartRate(userHealthIndex.getHeartRate());
                    existingUserHealthIndex.setBloodPressureSystolic(userHealthIndex.getBloodPressureSystolic());
                    existingUserHealthIndex.setBloodPressureDiastolic(userHealthIndex.getBloodPressureDiastolic());
                    UserHealthIndex updatedUserHealthIndex = userHealthIndexService.save(existingUserHealthIndex);
                    return new ResponseEntity<>(updatedUserHealthIndex, HttpStatus.OK);
                })
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUserHealthIndex(@PathVariable Long id) {
        if (userHealthIndexService.findById(id).isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        userHealthIndexService.deleteById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @GetMapping("/user/{userId}")
    public ResponseEntity<List<UserHealthIndex>> getUserHealthIndicesByUserId(@PathVariable Long userId) {
        List<UserHealthIndex> userHealthIndices = userHealthIndexService.findByUserId(userId);
        if (userHealthIndices.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(userHealthIndices, HttpStatus.OK);
    }
} 